package com.tmt.im.jpa.repository;

import com.tmt.data.jpa.repository.ITmtBaseRepo;
import com.tmt.im.jpa.entity.IMFriend;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Description TODO
 * @Author wuyi
 * @Date 2024/7/13 19:03
 * @Version 1.0
 **/
@Repository
public interface IMFriendRepo extends ITmtBaseRepo<IMFriend, Long> {

    Page<IMFriend> findAllByOwnerId(Long ownerId, Pageable pageable);

    boolean existsByOwnerIdAndFriendId(Long ownerId, Long friendId);

    List<IMFriend> findAllByIdIn(List<Long> ids);

    void deleteAllByFriendApplyIdIn(List<Long> applyIds);

    @Query("select t from IMFriend t where (t.owner.id = :ownerId) and (" +
            "(:alias is not null and t.alias is not null and t.alias like (:alias)) " +
            "or " +
            "(:nickname is not null and t.friend.nickname is not null and t.friend.nickname like(:nickname))" +
            ") order by t.createTime desc")
    Page<IMFriend> queryAllBy(@Param("ownerId") Long ownerId,
                                                 @Param("alias") String alias,
                                                 @Param("nickname") String nickname,
                                                 Pageable pageable);

}
